﻿//    Copyright (C) 2013 Jay Herman

//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APKExtractor
{
    class Program
    {
        [STAThreadAttribute]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();

            ORTS.Menu.Program.Initialize(); 

            if (args.Length < 1)
            {
                Console.WriteLine("Usage: APKExtractor filename");
                return; 
            }

            string fileName = args[0];
            _APKFile APKFile = new _APKFile(fileName);
            if (args.Length > 1 && args[1] == "-zip")
            {
                string zipName = fileName.Substring(0, fileName.Length - 3) + "zip";
                APKFile.ExportToZipFile(zipName);
            }
            else
            {
                string directory = null;

                if (args.Length > 1)
                    directory = args[1];

                if (directory == null)
                    FolderHelper.TryCurrentFolder(APKFile.FolderName, ref directory);

                if (directory == null)
                    FolderHelper.TryOpenRailsFolder(APKFile.FolderName, ref directory);

                if (directory != null)
                {
                    string message = string.Format("Unpackage activity to directory: {0}?", directory);
                    DialogResult result = MessageBox.Show(message, "Unpackage Activity?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    switch (result)
                    {
                        case DialogResult.Yes:
                            {
                                APKFile.ExportToDirectory(directory);
                                break;
                            }
                        case DialogResult.No:
                            {
                                using (FolderPicker folderPicker = new FolderPicker())
                                {
                                    if (folderPicker.ShowDialog() == DialogResult.OK)
                                        APKFile.ExportToDirectory(folderPicker.FolderPicked);
                                }
                                break;
                            }
                    }
                }
                else
                {
                    string message = string.Format("The route {0} ({1}) can not be found in any of your default locations. " +
                        "Would you like to choose a location to extract the activity?", APKFile.RouteName, APKFile.FolderName);
                    DialogResult result = MessageBox.Show(message, "Extract activity manually?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        using (FolderPicker folderPicker = new FolderPicker())
                        {
                            if (folderPicker.ShowDialog() == DialogResult.OK)
                                APKFile.ExportToDirectory(folderPicker.FolderPicked);
                        }
                    }
                }
            }
        }
    }
}
